package edu.sjsu.cs.queryopt.dbms.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import edu.sjsu.cs.queryopt.dbms.dao.Index;

public class CreateTable {
        static Index i = new Index();
        static ArrayList<Integer> NewOrder  = new ArrayList<Integer>();
         
        /***
         *
         *
         * @param: readTableFile not using it maybe future
         * @param: pod is containing index name table name and col number, dirtection
         * @param:TextFileName : table name
         * @param: numberofcal how name col we have in the tabl
         * ACS = 1 DCS = 2
         * **/
        public static ArrayList<String> Createtb(Scanner readTableFile , ArrayList<String> pod,
                        String TextFileName, int numberofcal , ArrayList<Integer> valueandtype) throws FileNotFoundException
        {
               
                int devidor =numberofcal; // tells how many time we need to acs or dec
                ArrayList <String> al = new ArrayList<String>();
                ArrayList <String> dummy = new ArrayList<String>(); // we sort this
                ArrayList<Integer> orderList  = new ArrayList<Integer>(); // for result
                ArrayList<Integer> getColValue = new  ArrayList<Integer>();
                getColValue. addAll(TableHelper.getColValue(valueandtype));
                ArrayList<String> oneColume  = new ArrayList<String>(); // for result
                ArrayList<String> copylist  = new ArrayList<String>(); // for result
                ArrayList<String> AppendList  = new ArrayList<String>(); // for result
                Hashtable<String, Integer> Howmanytimerepeat = new Hashtable<String, Integer>();
                Hashtable<String, String> wordspod = new Hashtable<String, String>();
                wordspod = Helper.BuildWordpod();
                       
                al = MappingOrignalTable(TextFileName);
            
//                System.out.println("al Howmanytimerepeat " + al);
        //        Howmanytimerepeat =howmanySameWord(al, (((int) pod.get(2).charAt(0))-48) ,(valueandtype.size()/2));
               
             //   dummy = preventDuplicate(al , (((int) pod.get(2).charAt(0))-48) ,  (valueandtype.size()/2) );
                int mod =  (valueandtype.size()/2);
                int something = (((int) pod.get(2).charAt(0))-48) -1 ;
                for(int un = 0 ;  un < dummy.size() ; un++)
                {              
                        al.set(something , dummy.get(un));
                        something = something + mod;            
                }
                //System.out.println("wordspod " + wordspod);
//                System.out.println("pod " + pod);
//                System.out.println("al " + al);
//                System.out.println("valyetype " + valueandtype);
//                System.out.println("dummy " + dummy);
//                System.out.println("getColValue " + getColValue);
                // ArrayList<Integer> GetNeedPodValue = new ArrayList<Integer>();
                // GetNeedPodValue = GetPodValueforSort(pod , valueandtype);
                // asending or desending based on how many times
                // for example pod [X2, T2, 1A, 2D] then size is 4
                // but we dont need X2 and T2 so SIZE-2 will be the
                // number that we want to loop to acs or dec
                for(int index = 0 ; index < (pod.size()-2) ; index++)
                {
                        //if(TableHelper.returnColFromIndex(pod, index))
//                        System.out.println("acs or dec " + Helper.isACSorDSC(pod.get(index+2)));
                        if(Helper.isACSorDSC(pod.get(index+2))== 1)
                        {// ACS
//                              System.out.println("TableHelper.returnColFromIndex(pod, index+1) " +   (TableHelper.returnColFromIndex(pod, index)-1) );
                                if(getColValue.get(((TableHelper.returnColFromIndex(pod, index+1)-1)))==1)
                                {// char
//                                        System.out.println("col in the char " + index );
//                                      System.out.println("col number "  +(((int) pod.get(index+2).charAt(0))-48) );
                                       
                                        if(index ==0)
                                        {
//                                        		System.out.println("first acs numberofcal " +numberofcal);
                                                oneColume = GetOneColumn(al, (TableHelper.returnColFromIndex(pod, index+1)-1) +1 , numberofcal );
//                                                System.out.println("ACS First oneColume " + oneColume);
                                         //       oneColume = killFrontIndex(oneColume);
                                        //      oneColume = SwitchAllAlphabet(oneColume, wordspod);
                                        //      orderList = NewSorting(oneColume,Howmanytimerepeat);
                                                //Collections.reverse(orderList);
//                                                System.out.println("ACS First reverce oneColume 1 " + oneColume);
                                               
                                                oneColume = appendEmptySpace(oneColume , valueandtype , (((int) pod.get(index+2).charAt(0))-48));
//                                                System.out.println("first char oneColume add empty " + oneColume);
                                        //      copylist = SwitchAllAlphabet(oneColume, wordspod);
//                                                System.out.println("ACS First empty oneColume " + oneColume);
                                                copylist = oneColume;
                                               

                                        }
                                        else
                                        {
//                                                System.out.println("second char col!!!");
//                                                System.out.println("second acs numberofcal " +numberofcal);
                                                oneColume = GetOneColumn(al, (TableHelper.returnColFromIndex(pod, index+1)-1) +1 , numberofcal );
                                                oneColume = appendEmptySpace(oneColume , valueandtype , (((int) pod.get(index+2).charAt(0))-48));
                                                //AppendList = SwitchAllAlphabet(oneColume, wordspod);
                                                copylist = combineTwoList(copylist ,  oneColume);
//                                                System.out.println("ACS second char copylist!! " + copylist);

                                        }
                                       // oneColume.clear();
                                }
                                else
                                {// int
//                                      System.out.println("col in the int dfasdfas " + index );
                                        if(index == 0)
                                        {
//                                                System.out.println("First int DEC");
                                                //CalDesIntValue();
//                                                System.out.println("first int acs numberofcal " +numberofcal);
                                                oneColume = GetOneColumn(al, (TableHelper.returnColFromIndex(pod, index+1)-1) +1 , numberofcal );
                                        //      orderList = NewSorting(oneColume,Howmanytimerepeat);
                                        //      Collections.reverse(orderList);
                                        //        oneColume = killFrontIndex(oneColume);
                                               
//                                                System.out.println("DEV col in the int oneColume " + oneColume );
//                                              NewOrder =
//                                                              FindNewOrder(al,oneColume ,
//                                                                              (((int) pod.get(index+2).charAt(0))-48) ,
//                                                                                              Helper.isACSorDSC(pod.get(index+2)), (valueandtype.size())/2 );
//                                              copylist = CalDesIntValue(oneColume);
                                                copylist = appendZero(oneColume);
                                               // System.out.println("col in the int copylist " + copylist );

                                        }
                                        else
                                        {

//                                                System.out.println("Second int DEC");
//                                                System.out.println("second int acs numberofcal " +numberofcal);
                                                oneColume = GetOneColumn(al, (TableHelper.returnColFromIndex(pod, index+1)-1) +1 , numberofcal );
//                                                System.out.println("DEC second int copylist!! " + oneColume);
                                                //oneColume = killFrontIndex(oneColume);
                                                AppendList = appendZero(oneColume);
//                                                System.out.println("DEC second int AppendList!! " + AppendList);
//                                                System.out.println(" DEC before second int copylist!! " + copylist);
                                                copylist = combineTwoList(copylist ,  AppendList);
//                                                System.out.println("DEC second int copylist!! " + copylist);
                                               
                                        }
                                       // oneColume.clear();
                                       
                                }
                               
                               
                        }
                        else
                        {// DCS
//                                System.out.println("Welcome to DCE World");
                                if(getColValue.get(((TableHelper.returnColFromIndex(pod, index+1)-1)))==1)
                                {// char
//                                      System.out.println("col in the char " + index );
//                                      System.out.println("col number "  +(((int) pod.get(index+2).charAt(0))-48) );
                                       
                                        if(index ==0)
                                        {
//                                        	System.out.println("first char sec numberofcal " +numberofcal);    
                                        	oneColume = GetOneColumn(al, (TableHelper.returnColFromIndex(pod, index+1)-1) +1 , numberofcal );
//                                                System.out.println("Dis First oneColume " + oneColume);
                                         //       oneColume = killFrontIndex(oneColume);
                                                oneColume = SwitchAllAlphabet(oneColume, wordspod);
                                        //      orderList = NewSorting(oneColume,Howmanytimerepeat);
                                                //Collections.reverse(orderList);
//                                                System.out.println("Dis First reverce oneColume 1 " + oneColume);
                                               
                                                oneColume = appendEmptySpace(oneColume , valueandtype , (((int) pod.get(index+2).charAt(0))-48));
//                                                System.out.println("first char oneColume add empty " + oneColume);
                                        //      copylist = SwitchAllAlphabet(oneColume, wordspod);
//                                                System.out.println("Dis First empty oneColume " + oneColume);
                                                copylist = oneColume;
//                                              System.out.println("Dis First after kill index " + copylist);
//                                              Collections.sort(oneColume);
//                                              Collections.reverse(oneColume);
//                                              System.out.println("reverced " + oneColume);
                                        }
                                        else
                                        {
//                                                System.out.println("second char col!!!");
//                                                System.out.println("second char sec numberofcal " +numberofcal); 
                                                oneColume = GetOneColumn(al, (TableHelper.returnColFromIndex(pod, index+1)-1) +1 , numberofcal );
                                                oneColume = appendEmptySpace(oneColume , valueandtype , (((int) pod.get(index+2).charAt(0))-48));
                                                AppendList = SwitchAllAlphabet(oneColume, wordspod);
                                                copylist = combineTwoList(copylist ,  AppendList);
//                                                System.out.println("second char copylist!! " + copylist);
                                        }
                                      //  oneColume.clear();
                                }
                                else
                                {// int
//                                      System.out.println("col in the int dfasdfas " + index );
                                        if(index == 0)
                                        {
//                                                System.out.println("First int DEC");
//                                                System.out.println("first int sec numberofcal " +numberofcal); 
                                                //CalDesIntValue();
                                                oneColume = GetOneColumn(al, (TableHelper.returnColFromIndex(pod, index+1)-1) +1 , numberofcal );
                                        //      orderList = NewSorting(oneColume,Howmanytimerepeat);
                                        //      Collections.reverse(orderList);
                                        //        oneColume = killFrontIndex(oneColume);
                                               
//                                                System.out.println("col in the int oneColume " + oneColume );
//                                              NewOrder =
//                                                              FindNewOrder(al,oneColume ,
//                                                                              (((int) pod.get(index+2).charAt(0))-48) ,
//                                                                                              Helper.isACSorDSC(pod.get(index+2)), (valueandtype.size())/2 );
                                                copylist = CalDesIntValue(oneColume);
//                                                System.out.println("col in the int copylist " + copylist );
                                        }
                                        else
                                        {
//                                                System.out.println("Second int DEC");
//                                                System.out.println("second int sec numberofcal " +numberofcal); 
                                                oneColume = GetOneColumn(al, (TableHelper.returnColFromIndex(pod, index+1)-1) +1 , numberofcal );
//                                                System.out.println("second int copylist!! " + oneColume);
                                                //oneColume = killFrontIndex(oneColume);
                                                AppendList = CalDesIntValue(oneColume);
//                                                System.out.println("second int AppendList!! " + AppendList);
//                                                System.out.println("before second int copylist!! " + copylist);
                                                copylist = combineTwoList(copylist ,  AppendList);
//                                                System.out.println("second int copylist!! " + copylist);
                                        }
                                     //   oneColume.clear();
                                       
                                }

                        }
                }
               
               
//                if(Helper.isACSorDSC(pod.get(2))== 1){
//                        System.out.println("ACS FIRST");
//                                for(int inum = 0 ; inum < orderList.size() ; inum++)
//                                {
//                                        NewOrder.add(orderList.get(inum) +1);
//                                }
//                }
//                else{
//                        System.out.println("DCS FIRST");
                                 HashMap<Integer , String> mapht = remappingForresult(copylist , pod.size() -2);
                                 String [] countries = copylist.toArray(new String[copylist.size()]);
                                 Arrays.sort(countries, String.CASE_INSENSITIVE_ORDER);
                                 ArrayList<String> forcopy = new ArrayList<String>();
                                 for(int iiii = 0 ; iiii< countries.length ;iiii++)
                                 {
                                        forcopy.add(countries[iiii]);
                                 }
                //               System.out.println("mapht " + mapht);
                                 Hashtable<Integer, Integer> orderht = new Hashtable<Integer, Integer>();
                                 int indexnumber = 0;
                                 for(int outer = 0 ; outer < countries.length ; outer++)
                                 {
                                         for(int inner = 0 ; inner < mapht.size() ; inner++)
                                         {
                                                 if(countries[outer].equals(mapht.get(inner)))
                                                 {
                                                         orderht.put(outer, inner+1);
                                                         mapht.put(inner, "-9999999999-999");
                                                         break;
                                                       
                                                 }
                                         }
                                 }
                                 // need to reverce
//                                 System.out.println("maphtmaphtmapht " + mapht );
                                 for(int aaaa = orderht.size()-1 ; aaaa >= 0  ; aaaa--)
                                 {
                                         NewOrder.add(orderht.get(aaaa));
                                 }
                                 copylist = forcopy;
                                 Collections.reverse(NewOrder);
//                                 System.out.println("NewOrderNewOrder " + NewOrder);
                               
//                }
               
                 
//                 System.out.println("copylistcopylistcopylist " + copylist);
                return copylist;
               
               
               
        }
        /**
         * al = copylist
         * colNum = pod.size() -2
         * */
        public static HashMap<Integer, String> remappingForresult(ArrayList<String> al , int colNum)
        {
                String makeSentance = "";
//                System.out.println("remappingForresult al " + al);
//                System.out.println("remappingForresult colNum " + colNum);
                HashMap<Integer, String> ht = new HashMap<Integer , String>();
                Hashtable<Integer , String> reht = new Hashtable<Integer , String>();
                int counter = 1;
                int indexer = 0;
                for(int index = 0 ; index < al.size() ; index++)
                {                      
                                ht.put(index, al.get(index));                                  
                }
//                System.out.println("remappingForresult " + ht);
               

                return ht;
               
        }
       
        public static ArrayList<String> SwitchAllAlphabet(ArrayList<String> words , Hashtable<String, String> wordpod)
        {
                ArrayList<String> retureArray = new ArrayList<String>();
                String targetWord = "";
                String resultWord = "";
//              System.out.println("SwitchAllAlphabet words " + words);
//              System.out.println("SwitchAllAlphabet wordpod " + wordpod);
                for(int a = 0 ; a < words.size() ; a ++)
                {
//                      System.out.println("a");
                        targetWord = words.get(a);
//                      System.out.println("targetWord.length() " + targetWord.length());
                        for(int b = 0 ; b < targetWord.length() ; b++)
                        {
                                //System.out.println("b");
                                String oneChar = Character.toString(targetWord.charAt(b));
                                if(wordpod.contains(oneChar)){
                                resultWord += wordpod.get(oneChar);
                                }
                                else{
                                        resultWord += oneChar;
                                }
                        }
                       
                        retureArray.add(resultWord);
                        resultWord = "";
                       
                }
//                System.out.println ("SwitchAllAlphabet :After convert " + retureArray);
                return retureArray;
        }
        public static ArrayList<String> killFrontIndex(ArrayList<String> copylist )
        {
                String finalSting = "";
               
                ArrayList<String> ar =  new ArrayList<String>();
                int leng = 0 ;
                for(int index = 0 ; index < copylist.size() ; index++)
                {
                        String sentence = copylist.get(index);
                        while((int)sentence.charAt(leng) != 32)
                        {
                                leng++;
                        //      System.out.println("oneline sdas : " + sentence.substring(leng, sentence.length()));
                                finalSting = sentence.substring(leng, sentence.length());
               
                        }
                       
                        finalSting = finalSting.trim();
                        ar.add(finalSting);
                        leng = 0 ;
                }
                for(int x = 0 ; x <copylist.size() ; x++)
                {
                	ar.add(x, copylist.get(x));
                }
                System.out.println ("kill "  + ar);
                return ar;
               
        }
        public static ArrayList<String> GerrateResult(ArrayList<String> copylist , ArrayList<Integer>  orderlist)
        {
//                System.out.println("GerrateResult : copylist " + copylist);
//                System.out.println("GerrateResult : orderlist " + orderlist);
                String [] resultArray =  new String [copylist.size()];
                //ArrayList<String> returnList = new ArrayList<String>();
                for(int index = 0 ; index < copylist.size() ; index++)
                {
//                      System.out.println("orderlist.get(index) " + orderlist.get(index)+ " <- " + copylist.get(index) + " index " + index);
                        resultArray[index] = copylist.get(orderlist.get(index));
                }
                ArrayList<String> returnList = new ArrayList(Arrays.asList(resultArray));
//              System.out.println("Final list gg " + returnList);
                return returnList;
               
        }
       
        public static Hashtable< String, Integer> howmanySameWord(ArrayList al , int firstCol , int numberofcol)
        {
                ArrayList<String> onecol = GetOneColumn(al ,firstCol , numberofcol  );
                Hashtable <String, Integer> ht =  new Hashtable<String, Integer>();
                Collections.sort(onecol);
               
                ArrayList<Integer> als =  new ArrayList<Integer>();
                List<String> list = new ArrayList<String>(onecol);
               
                Set<String> unique = new HashSet<String>(list);
                for (String key : unique) {
                        ht.put(key, Collections.frequency(list, key));
                  //  System.out.println(key + ": " + Collections.frequency(list, key));
                }
               
//            System.out.println("dfsadf ht" +  ht);
           
                return ht;
        }
     
        /**
         *
         *
         * */
       
        public static ArrayList<String> preventDuplicate (ArrayList<String> al , int firstColforACSorDCS , int sizeOfRow)
        {
                ArrayList<String> AddIndex =    GetOneColumn(al , firstColforACSorDCS ,sizeOfRow);
                String newValue = "";
                for(int index = 0 ; index < AddIndex.size() ; index ++)
                {
//                      System.out.println("index sdasd " +  index);
                        newValue = String.valueOf(index) + " " + AddIndex.get(index);
//                      System.out.println("new value " + newValue);
                        AddIndex.set(index, newValue);
                }
//              System.out.println("new list " +  AddIndex);
                return AddIndex;
               
       
        }
       
        /**
         *
         *    @param OrignalCopy orignal copy of one col: we need to re --arrange this col
         *    @param new order that we want to re-arrange
         *    
         *    For example orderlist [4, 3, 1, 2] , OrignalCopy [John  , Jane  , Ku    , AA    ]
         *    then we need to move 4th element to first   , 3th element to second , first element to third
         *    and second element to second
         * */
        public static ArrayList<String> makeRightOrder(ArrayList<String> OrignalCopy , ArrayList<Integer> orderlist)
        {
                String [] copyArray =  new String[orderlist.size()];
//              System.out.println("orderlist " + orderlist);
//              System.out.println("OrignalCopy " + OrignalCopy);
                for(int index = 0 ; index < orderlist.size() ; index++)
                {
//                      System.out.println("******************");
//                      System.out.println("orderlist.get(index)-1  in " + (orderlist.get(index)));
//                      System.out.println(" OrignalCopy.get(index) " +  OrignalCopy.get(orderlist.get(index)-1));
                        copyArray[index] = OrignalCopy.get(orderlist.get(index)-1);
//                      System.out.println("******************");
//                      System.out.println();
                }
                ArrayList<String> list = new ArrayList<String>(Arrays.asList(copyArray));
               
//              for(int i = 0 ; i < 3 ; i++)
//              {
//                      System.out.println(copyArray[i]);
//              }
//                System.out.println("list dfs " + list);
                return list;
        }
        public static ArrayList<String>  combineTwoList(ArrayList<String> OrignalCopy , ArrayList<String> addingList)
        {
                ArrayList<String> returnList = new ArrayList<String>();
                String resultString = "";
                for(int index = 0 ; index < OrignalCopy.size() ; index++)
                {
                        resultString  = OrignalCopy.get(index)  + addingList.get(index);
                        returnList.add(index , resultString);
                }
                return returnList;
               
        }
        public static ArrayList<Integer> FindNewOrder(ArrayList<String> OrignalCopy
                                , ArrayList<String> sortedCopy , int numberofcol, int AcsDec , int totalcolintable)
        {
                ArrayList<Integer> orderList = new ArrayList<Integer>();
                ArrayList<String> oneCol = GetOneColumn(OrignalCopy, numberofcol , totalcolintable);
                boolean flag = false;
               
//              System.out.println("sortedCopy " + sortedCopy);
//              System.out.println("numberofcol " + numberofcol);
//              System.out.println("AcsDec " + AcsDec);
//              System.out.println("oneCol " + oneCol);
                if(AcsDec == 1){
                for(int i  = 0 ; i < sortedCopy.size() ; i++)
                {
                        for(int ii = 0 ; ii < oneCol.size(); ii++)
                        {

                       
                                if(oneCol.get(ii).equals(sortedCopy.get(i)))
                                {
//                                      System.out.println("A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//                                      System.out.println("oneCol " + oneCol.get(ii) + " " + " ii "+ii);
//                                      System.out.println("vs");
//                                      System.out.println("ACS:sortedCopy "+sortedCopy.get(i) + " " +" i " + i);
//                                      System.out.println("A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
                                        orderList.add(ii+1);

                                }
                                //System.out.println("orderList " + orderList);
                        }
                       
                }
                }
                else
                {
//                        System.out.println("I am in DEC");
                        for(int ii = 0 ; ii <sortedCopy.size() ; ii++ )
                        {
                                for(int i = 0 ;  i < OrignalCopy.size() ; i++)
                                {
//                                      System.out.println("D%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//                                      System.out.println("DCS: sortedCopy " + sortedCopy.get(ii) + " " + " i "+ii);
//                                      System.out.println("vs");
//                                      System.out.println("DCS:OrignalCopy "+ OrignalCopy.get(i) + " " +" i " + i);
//                                      System.out.println("D%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
                                        if(OrignalCopy.get(i).equals(sortedCopy.get(ii)))
                                        {
                                               
                                               
                                                if(i == 0)
                                                {
                                                        orderList.add(1);
                                                       
                                                }
                                                else{
                                                orderList.add((numberofcol*i)+1);
                                                }
                                               
                                        }
                                       
                                }
                               
                        }
                       
                }
               
                // Collections.reverse(orderList);
               
//              System.out.println("FindNewOrder " + orderList);
               
                return orderList;      
        }
       
        /**
         * @param al : dummy array get data from here and sort by ACS or DCS
         * @param Direction: ACS or DCS
         * @param colNum: target that change direction
         *
         * Collections.sort() -> change to ACS order
         * Collections.reverse() -> change to DCS order
         * for DCS int value we use ConvertStringToInt to calculate 9999999999 - actual value
         *
         * **/
       
        public static ArrayList<String>
                SortOneCol(ArrayList<String> al  , int Direction , int colNum , int dividor)
        {
                ArrayList <String> re = new ArrayList<String>();
                ArrayList <String> re2 = new ArrayList<String>();
                boolean isInt = false;
                re = GetOneColumn(al , colNum,  dividor );
                isInt = TableHelper.isInt(al.get(colNum-1));
               
                if(isInt){
                        System.out.println("SortOneCol this is int");
                        if(Direction == 1)
                        {//ACS
                                System.out.println("SortOneCol this is int ACS");
                                Collections.sort(re);
                                re= appendZero(re);

                               
                        }
//                        else
//                        {//DCS
//                           Collections.sort(re);
//                           re = CalDesIntValue(re);              
//                        }
                }
                else
                {
                        System.out.println("SortOneCol this is char");
                        if(Direction == 1)
                        {//ACS need revrce
                                Collections.sort(re);
                               
                        }
                        else
                        {//DCS
                                System.out.println("SortOneCol this is dec");
                                Collections.sort(re);
                                Collections.reverse(re);
                        }
                       
                }
                System.out.println("SortOneCol :" +re);
                return re;
        }
        /**
         *
         * **/
//      public static ArrayList<String> appendEmptySpace(ArrayList<String> al ,
//                      ArrayList<Integer> whichColNeedSpace)
//      {
//              
//      }
//      
        //append number of zero for int value
        public static ArrayList<String> appendZero(ArrayList<String> all)
        {
                String ZeroString = "";
                ArrayList<String> al2 = new ArrayList<String>();
                ArrayList<String> alll = all;
                for(int a = 0 ; a < alll.size() ; a++)
                {
//                        if(alll.get(a).contentEquals("9999999999"))
//                        {
//                                alll.set(a, "9999999999");
//                        }
                    
                            if(alll.get(a).equals("9999999999") )
                            {
                                    alll.set(a, "9999999999");
                            }
                        	
                     
                       
                                               
                }
                for(int index=  0; index < alll.size() ; index++)
                {
                        if(alll.get(index).length() < 10)
                        {
                                for(int index2 = 0 ; index2 < (10 -alll.get(index).length() ) ; index2++)
                                {
                                        ZeroString += WordCapsule.QUC_ZERO;
                                }
//                              System.out.println("ZeroString " + ZeroString);
                        }
                        ZeroString = ZeroString + alll.get(index);
                        al2.add(ZeroString);
                        ZeroString = "";
                       
                }
//                System.out.println("what is reval ? " + al2);
                return al2;
        }
        // append empty spaces based on size of char
        /**
         * al: list that we want to append zero
         * valyetype : [1, 6, 2, 10, 1, 5]
         * colnumber: col number that we want to change
         * */
        public static ArrayList<String> appendEmptySpace(ArrayList<String> al , ArrayList<Integer> valyetype , int colnumber)
        {
                String emptySpace = "";
                String resultString = "";
                ArrayList<String> al2 = new ArrayList<String>();
                ArrayList<Integer> a = new ArrayList<Integer>();
               
                for(int i = 0 ;  i < valyetype.size() ; i++)
                {
                        if((i%2) != 0)
                        {
                                a.add(valyetype.get(i));
                        }
                       
                }
                int size = valyetype.get(colnumber + 2);
                for(int index = 0 ; index < al.size() ; index++)
                {
                        for(int i = 0 ; i <(a.get(colnumber-1) -al.get(index).length() ); i++)
                        {
                                emptySpace += WordCapsule.QUC_EMPTY;
                        }
                        resultString = al.get(index)+emptySpace;
                        al2.add(resultString);
                        emptySpace = "";
                }
                return al2;
               
               
        }
       
       
       
        /**
         * Using one col number and array to get only one col that we want to
         * sort
         * @param: calNum: it is cal number the we want to have
         * @param dividor: it is telling that next element that we want to put in arraylist
         * for example
         * we have [John , 31, Jane, 35]
         * and we want to get cal 1 then we need to put John and Jane to arraylist
         * so for loop is getting data which address has 0 , 3.
         *
         *
         * **/
        public static ArrayList<String> GetOneColumn(ArrayList<String> al , int calNum , int dividor)
        {
                ArrayList<String> copyArrayList = new ArrayList<String>();
                int wantCalNum = calNum-1;
//              System.out.println("al GetOneColumn " + al);
//              System.out.println("calNum GetOneColumn " + (calNum-1));
//              System.out.println("dividor GetOneColumn " + dividor);
                for(int index = 0 ; index < al.size() ; index++)
                {
                        if(index == wantCalNum)
                        {
                                copyArrayList.add(al.get(index));
                                wantCalNum = wantCalNum+ dividor;
                        }
                }
//        System.err.println("copyArrayList  GetOneColumn" + copyArrayList);
                return copyArrayList;
        }
        /**
         * Get Type of col
         * 
         * */
        
        public static ArrayList<String> GetColTypes(String nameOftable) throws FileNotFoundException{
        	  ArrayList <String> al = new ArrayList<String>();
              int index = 0;
              String EachLineInTable= "";
              String result  = "";
              Scanner readTableFile = new Scanner(new File(nameOftable));
              while(readTableFile.hasNext())
              {
                      EachLineInTable = readTableFile.nextLine();
                    //  System.out.println(EachLineInTable);
//                    System.out.println("inde x " + index);
                      if(index == 0)
                      {
//                            System.out.println("result " + result);
//                            System.out.println("EachLineInTable " + EachLineInTable);
                              result = EachLineInTable;
                      }
                      index++;
                     
              }
      //      System.out.println("result " + result);
              //put into array for all table contents
              al =Helper.StringToArrayList(result);
             
              return al;
             
        }
       
        /**
         * Using the while loop to get covert table file to arraylist
         * @throws FileNotFoundException
         * **/
        public static ArrayList<String> MappingOrignalTable(String nameOftable) throws FileNotFoundException
        {
                ArrayList <String> al = new ArrayList<String>();
                int index = 0;
                String EachLineInTable= "";
                String result  = "";
                File file =  new File(nameOftable);
                if(!file.isFile()){
                	System.err.println("MappingOrignalTable:CAN'T SELECT THIS FILE " + nameOftable);
                	return null;
                }
                Scanner readTableFile = new Scanner(file);
                while(readTableFile.hasNext())
                {
                        EachLineInTable = readTableFile.nextLine();
                      //  System.out.println(EachLineInTable);
//                      System.out.println("inde x " + index);
                        if(index > 1)
                        {
//                              System.out.println("result " + result);
//                              System.out.println("EachLineInTable " + EachLineInTable);
                                result = result + " " + EachLineInTable;
                        }
                        index++;
                       
                }
        //      System.out.println("result " + result);
                //put into array for all table contents
                al =Helper.StringToArrayList(result);
               
        return al;
               
        }
        public static String NumberOfCalIntable(String NameOfTable) throws FileNotFoundException
        {
        	  ArrayList <String> al = new ArrayList<String>();
              int index = 0;
              String EachLineInTable= "";
              String result  = "";
              Scanner readTableFile = new Scanner(new File(NameOfTable));
             while(readTableFile.hasNext())
             {
                     EachLineInTable = readTableFile.nextLine();
                    // System.out.println(EachLineInTable);
//                   System.out.println("inde x " + index);
                     if(index == 1)
                     {
//                           System.out.println("result " + result);
//                           System.out.println("EachLineInTable " + EachLineInTable);
                             result = EachLineInTable;
                     }
                   index++;
                    
             }
             System.out.println("result " + result);
             return result;
        }
       
       
        //adding 9 for DEC Part
        public static ArrayList<String> CalDesIntValue(ArrayList<String> al)
        {
                double calResult = 0;
                String digitsString = "";
                String ConvertIntToString = "";
                ArrayList <String> all = new ArrayList<String>();
                String newre = "";
                String newone = "";
                for(int index= 0; index <al.size() ; index++)
                {
                	if(al.get(index).equals(("9999999999")))
                	{
                		all.add("0000000000");
                	}
                	else{
                        calResult =TableHelper.ConvertStringToDouble(al.get(index));
                        calResult  = (9999999999.0 - calResult);                        
                     //   all.add(TableHelper.CovertDoubleValueWithOutE(calResult));                      
                       
                        newone = TableHelper.CovertDoubleValueWithOutE(calResult);
//                        System.out.println("digitsString " + digitsString);
                        if(newone.length() < 10)
                        {
//                        	System.out.println("Missing digit");
                        	//String missingzero= "0";
                        	for(int iii = newone.length() ; iii < 10 ; iii++)
                        	{
                        	//	missingzero
                        		newre += "0";
                        	}
                        	 newone  =  newre + newone;
                        	 newre = "";
                        }
                        calResult = 0;
//                        System.out.println("Insert new" + newone);
                        all.add(newone);
                        newone = " ";
                	}
                	
                }
//                System.out.println("digitsString " + digitsString);
//               System.out.println("allall " + all);

                
                return all;
        }

}
